import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.swing.JTable;

import com.mysql.jdbc.ResultSet;
import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;
/**
 * This class is Receive AssignmentUI
 */
public class ReceiveAssignmentUI extends JPanel {
	
	/**
	 * title for the current window  
	 */
	private JLabel rasntitle = null;
	/**
	 * no assignment message  
	 */
	private JLabel noassnmsg = null;
	/**
	 * back button
	 */
	public JButton btnBack = null;
	/**
	 * help button
	 */
	public JButton btnHelp = null;
	/**
	 * save the current panel for the "back" function 
	 */
	private JPanel curPanel;
	private ResultSet res = null;
	private String courseIDname;
	ArrayList<Assignment> l = new ArrayList<Assignment>();
	private String courseName;
	private MyFile myFile;
	private Assignment assn;
	private MyFile file;
	/**
	 * date variable 
	 */
	private int day;
	private int month;
	private int year;
	/**
	 * logout button
	 */
	public JButton btnLogout = null;
	
	
	public ReceiveAssignmentUI () {
		super();
		emptyinitialize();
	}

	public ReceiveAssignmentUI(Object msg) {
		
		  super();
		  l = (ArrayList) msg;
		  initialize();
	}

	private void initialize() {
		curPanel = this;
		
		rasntitle = new JLabel("Receive/Submit Assignment");
		rasntitle.setBounds(160, 28, 180, 14);

		this.setSize(400, 321);
		this.setLayout(null);
		
		this.add(rasntitle);
				
		int size = l.size();
		
		JButton[] b=new JButton[size];
		JLabel[] lb=new JLabel[size];
		JLabel[] lb2=new JLabel[size];
			int x=270, y=60, width=150, height=20, width1=90;
			int x1=20, x2=110;
			for (int i=0; i<size ; i++)
			{
				assn = l.get(i);
				Integer assnum = assn.getAssNumber();
				SQL s = new SQL (assn.getCourseID(),18);
				s.accept();
				Object msg = s.getMsg();
				System.out.println("this course : " + i + assn.getCourseID()); 
				//JLabel1
				lb[i] = new JLabel((String)msg);
				lb[i].setBounds(x1, y, width, height);
				curPanel.add(lb[i]);
				//JLabel2
				lb2[i] = new JLabel("Assignment No: " +assnum.toString());
				lb2[i].setBounds(x2, y, width, height);
				curPanel.add(lb2[i]);
				//JButton Open
				b[i] = new JButton("Open");
				curPanel.add(b[i]);
				b[i].setBounds(x-20, y, width1, height);
				b[i].addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) { 
						
						Object arr[]=new String[3];
						arr[0]=Global.logIn.getUserlogin();
						arr[1]=(assn.getCourseID());
						arr[2]=assn.getAssNumber().toString();
						SQL s1 = new SQL (arr,20);
						s1.accept();
						Save s = new Save((MyFile)s1.getMsg());
					}
				});
				this.add(b[i],null);
				this.setVisible(true);
				//JButton Open
				
				//JButton Sub
				b[i] = new JButton("Submit");
				curPanel.add(b[i]);
				b[i].setBounds(x+80, y, width1, height);
				b[i].addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
								
						Browse b = new Browse();
						file = b.getFile();
						String n = file.getName();
						String fileName = new String();
						fileName = assn.getCourseID()+"_"+assn.getAssNumber().toString()+"_"+Global.logIn.getUserlogin()+"_"+n;
						file.setName(fileName);
						System.out.println("file isssssssssssssssssssssssss" +file.getName()); 
						  
						  
						Date date = new Date();
						System.out.println("daaaaaaaaaaaaaaaaaate: " +date); 
						String[] day = date.toString().split(" ");
						
						//System.out.println("daaaaaaaaaaaaaaaaaate" +datex); 
						
						
						String date1 = day[2]+".1.2012";
						System.out.println("daaaaaaaaaaaaaaaaaate" +date1); 
						  
						
						if (file != null)
						{
							Sub submit = new Sub (assn.getCourseID(),Global.logIn.getUserlogin(),assn.getAssNumber(),date1,assn.getTeacherID(),file);
							SQL s1 = new SQL (submit,45);
							s1.accept();
						}
						
						
					}
				});
				this.add(b[i],null);
				this.setVisible(true);
				//JButton Sub
				y+=30;
				
			}
	
		btnBack = new JButton("Back");
		btnBack.setBounds(187, 253, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		
		btnLogout = new JButton("Logout");        //Logout button
		btnLogout.setBounds(10, 11, 80, 19);
		add(btnLogout);
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  LogoutWin logt = new LogoutWin();
				  System.out.println("user press Logout button");
			}
		});
		this.setVisible(true);
		
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose a Course and and click on ", "'Open' or 'Submit' button");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);

	}
	
	private void emptyinitialize() {
		curPanel = this;


		noassnmsg = new JLabel("No Assignments For This Student");
		noassnmsg.setBounds(133, 49, 270, 46);

		this.setSize(400, 321);
		this.setLayout(null);
		this.add(noassnmsg);
		
		
	
		btnBack = new JButton("Back");
		btnBack.setBounds(187, 253, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		
		btnLogout = new JButton("Logout");        //Logout button
		btnLogout.setBounds(10, 11, 80, 19);
		add(btnLogout);
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  LogoutWin logt = new LogoutWin();
				  System.out.println("user press Logout button");
			}
		});
		this.setVisible(true);
		
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose a Course and and click on ", "'Open' or 'Submit' button");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);
		
	}
}